﻿using System;
using System.IO;
using System.Windows.Forms;

namespace FolderHierarchy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                string path = dialog.SelectedPath;
                textBox1.Text = path;
                textBox1.Update();
                button2_Click(sender, e);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                treeView1.Nodes.Clear();
                string path = textBox1.Text.TrimEnd('\\');
                if (!checkBox1.Checked)
                    treeView1.ImageList = SystemCalls.getSmallImageList();
                else
                    treeView1.ImageList = null;
                //treeView1.StateImageList = SystemCalls.getSmallImageList();

                TreeNode parent = treeView1.Nodes.Add(path.Substring(path.LastIndexOf('\\') + 1));
                parent.ToolTipText = path;
                parent.ContextMenuStrip = contextMenuStrip1;
                createTree(parent);
                treeView1.ExpandAll();
            }
            else
            {
                MessageBox.Show("Folder Not Exists!", "Warning");
            }
        }

        private void createTree(TreeNode parent)
        {
            string[] folders;
            try
            {
                folders = Directory.GetDirectories(parent.ToolTipText + '\\');
            }
            catch (Exception e)
            {
                textBox2.Text += e.Message + "\r\n";
                return;
            }
            foreach (string folder in folders)
            {
                TreeNode node = parent.Nodes.Add(folder.Substring(folder.LastIndexOf('\\') + 1));
                node.ToolTipText = folder;
                node.ContextMenuStrip = contextMenuStrip1;
                createTree(node);
            }
            string[] files = Directory.GetFiles(parent.ToolTipText + '\\');
            foreach (string file in files)
            {
                TreeNode node = parent.Nodes.Add(file.Substring(file.LastIndexOf('\\') + 1));
                node.ToolTipText = file;
                if (!checkBox1.Checked)
                    node.ImageIndex = node.SelectedImageIndex = SystemCalls.getImageIndex(file);
                node.ContextMenuStrip = contextMenuStrip1;
            }
        }

        private void openFolder_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;

            string folderPath = node.ToolTipText;
            if (File.Exists(folderPath))
                folderPath = folderPath.Substring(0, folderPath.LastIndexOf('\\'));
            System.Diagnostics.Process.Start(folderPath);
        }

        private void properties_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;

            SystemCalls.showProperties(node.ToolTipText);
        }

        private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeView1.SelectedNode = e.Node;
        }

        private void TreeView1_NodeMouseDoubleClick(object sender, System.Windows.Forms.TreeNodeMouseClickEventArgs e)
        {
            if (File.Exists(e.Node.ToolTipText))
                try
                {
                    System.Diagnostics.Process.Start(e.Node.ToolTipText);
                }
                catch (Exception exc)
                {
                    textBox2.Text += exc.Message + "\r\n";
                }
        }

    }
}
